package com.zhao.controller;

import com.zhao.dao.DepartmentDao;
import com.zhao.dao.MajorDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.*;
import com.zhao.service.DepartmentService;
import com.zhao.service.SchoolCounsellorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import java.util.List;

@Controller
public class SchoolCounsellorController {

    @Autowired
    private SchoolCounsellorService schoolCounsellorService;

    @Autowired
    private UniversityDao universityDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private DepartmentService departmentService;

    @ResponseBody
    @RequestMapping("admin-updateSchoolCounsellor.do")
    private String adminUpdateSchoolCounsellor(String schoolCounsellorID,String name,String department,String major,
                                               String password,String email,String phoneNumber,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = schoolCounsellorService.adminUpdateSchoolCounsellor(admin,schoolCounsellorID,name,department,major,password,email,phoneNumber,session);
        System.out.println(res);

        return res;
    }

    @ResponseBody
    @RequestMapping("admin-getOneStudent.do")
    public String adminGetOneStudent(HttpSession session,String schoolCounsellorID){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = schoolCounsellorService.adminGetOne(admin,schoolCounsellorID,session);
        System.out.println(res);

        return res;
    }

    @RequestMapping("admin-getAllSchoolCounsellor.do")
    public String adminGetAllSchoolCounsellor(HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = schoolCounsellorService.getAll(admin.getUniversityID(),session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-allSchoolCounsellor";
    }

    @RequestMapping("toAdmin-addSchoolCounsellor.do")
    public String toAdminAddStudents(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = departmentService.getAllByUniversityID(admin.getUniversityID(),session);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-addSchoolCounsellor";
    }

    @ResponseBody
    @RequestMapping("admin-addOneSchoolCounsellor.do")
    public String addOneSchoolCounsellor(String departmentID, String majorID, String name, String schoolCounsellorID,
                                         String gender, String password, HttpSession session){

        Admin admin = (Admin) session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }

        SchoolCounsellor schoolCounsellor = new SchoolCounsellor();
        schoolCounsellor.setUniversityID(admin.getUniversityID());
        schoolCounsellor.setDepartmentID(departmentID);
        schoolCounsellor.setMajorID(majorID);
        schoolCounsellor.setName(name);
        schoolCounsellor.setSchoolCounsellorID(schoolCounsellorID);
        schoolCounsellor.setGender(gender);
        if(password.equals("")){
            schoolCounsellor.setPassword("123");
        }else {
            schoolCounsellor.setPassword(password);
        }
        System.out.println(schoolCounsellor);
        String res = schoolCounsellorService.addOneSchoolCounsellor(schoolCounsellor,session);
        System.out.println(res);
        if(!res.equals("success")){
            return res;
        }


        return "success";
    }

    @RequestMapping("getSchoolCounsellorInfo.do")
    public String getSchoolCounsellorInfo(HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            return "redirect:/";
        }
        SchoolCounsellorImpl schoolCounsellorImpl = new SchoolCounsellorImpl();
        schoolCounsellorImpl.setSchoolCounsellorID(schoolCounsellor.getSchoolCounsellorID());
        schoolCounsellorImpl.setName(schoolCounsellor.getName());
        schoolCounsellorImpl.setUniversityID(schoolCounsellor.getUniversityID());
        University university = universityDao.findByID(schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setUniversityName(university.getUniversityName());
        schoolCounsellorImpl.setDepartmentID(schoolCounsellor.getDepartmentID());

        Department department = departmentDao.findByID(schoolCounsellor.getDepartmentID(),schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setDepartmentName(department.getDepartmentName());

        schoolCounsellorImpl.setEmail(schoolCounsellor.getEmail());
        schoolCounsellorImpl.setGender(schoolCounsellor.getGender());
        schoolCounsellorImpl.setImage(schoolCounsellor.getImage());
        schoolCounsellorImpl.setMajorID(schoolCounsellor.getMajorID());
        Major major = majorDao.findByID(schoolCounsellor.getMajorID(),schoolCounsellor.getUniversityID());
        schoolCounsellorImpl.setMajorName(major.getMajorName());

        schoolCounsellorImpl.setIsRegisted(schoolCounsellor.getIsRegisted());
        schoolCounsellorImpl.setPassword(schoolCounsellor.getPassword());
        schoolCounsellorImpl.setPhoneNumber(schoolCounsellor.getPhoneNumber());

        session.setAttribute("schoolCounsellorImpl",schoolCounsellorImpl);

        return "redirect:/newSchoolCounsellorFinishInfo";

    }

    @ResponseBody
    @RequestMapping("schoolCounsellorFinishInfo.do")
    public String schoolCounsellorFinishInfo(@RequestParam("email")String email,
                                             @RequestParam("phoneNumber")String phoneNumber,
                                             MultipartFile image,
                                             HttpSession session){

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor==null){
            return "redirect:/";
        }

        schoolCounsellor.setEmail(email);
        schoolCounsellor.setPhoneNumber(phoneNumber);
        System.out.println(schoolCounsellor);
        System.out.println(image.getOriginalFilename());

        String res = schoolCounsellorService.schoolCounsellorFinishInfo(schoolCounsellor,image,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "error";
        }

        return "success";
    }

}
